$().ready(function(){
    let page = {
        p:1,
        num:10,
        status: 4
    }
    function init(){
        getGoodsList(page.p,page.status);
    }
    init();

    //切换处理结果
    $('#goods_class').on('change',function(){
        let status = $(this).children('option:selected').val();
        page.status = status;
        getGoodsList(page.p,status);
    });

    //获取列表
    var goodslist = ''
    function getGoodsList(p,status){
        getAjax(`${global.apiUrl}/Api/Communityhealth/get_community_health_shop_product_new`,res=>{
            console.log(res);
            let html = '';
            goodslist = res.list
            let list = res.list;
            if(res.list.length != 0){
                $('#page').paging({
                    totalSize:res.count,
                    totalPage:Math.ceil(res.count/page.num),
                    pageNo:page.p,
                    callback(num){
                        page.p = num;
                        getGoodsList(num,page.status,page.service_class)
                    }
                })
                for(let i in list){
                    html += `
                            <tr>
                                <td class="shop-img">
                                    <img src="${list[i].head_img}" alt="">
                                </td>
                                <td>${list[i].product_name}</td>
                                <td>${list[i].content}</td>
                                <td>${list[i].gg}</td>
                                <td>${list[i].price}</td>
                                <td>${list[i].pay_no}</td>
                                <td>${list[i].pay_yes?list[i].pay_yes:0}</td>
                                <td class="control-td themeColor">
                                    <span class="reset" data-toggle="modal" data-target="#goodsModal" data-id="${list[i].id}" data-type="2">查看详情</span>
                                    ${getStatus(list[i].status,list[i].id)}
                                    ${getStatus2(list[i].status,list[i].id)}
                                </td>
                            </tr>
                        `
                }

            }else{
                $('#page').paging({
                    totalSize:0,
                    totalPage:0,
                    pageNo:0,
                    callback(num){
                        getGoodsList(num,page.status,page.service_class)
                    }
                })
                html = `
                    <tr>
                        <td colspan="8">暂无商品数据</td>
                    </tr>
                `
            }
            $('#goodsList').html(html);
        },{
            p:p,
            num:page.num,
            community_id: global.community_id,
            status:status, 
        })
    }

    function getStatus(status,id){
        var html = ''
        switch (status) {
            case '4':
                html += `<span class="select" data-id="${id}" data-type="1" style="margin-left: 15px">同意</span>`
                break;
            case '1':
                html += `<span class="select" data-id="${id}" data-type="0" style="margin-left: 15px">取消设为精选商品</span>`
                break;
        }
        return html
    }

    function getStatus2(status,id){
        var html = ''
        switch (status) {
            case '4':
                html += `<span class="select" data-id="${id}" data-type="0" style="margin-left: 15px">拒绝</span>`
                break;
        }
        return html
    }

    $('#goodsModal').on('show.bs.modal',function(event){//未完
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var goodsId = btnThis.data('id');
        var goodsInfo = '';
        for(var i in goodslist){
            if(goodslist[i].id == goodsId){
                goodsInfo = goodslist[i]
            }
        }
        var html = ''
        html += `<div class="shopItemAre">
                    <img src="${goodsInfo.head_img}" class="shopImg"></img>
                    <div class="shopItem">
                        <div>${goodsInfo.product_name}</div>
                        <div style="margin-top: 10px;">${'数量：'+ goodsInfo.pay_no}</div>
                    </div>
                </div>`
        $(".shopMainAre").html(html)
        $('.price').text(goodsInfo.price)
        $('.specifications').text(goodsInfo.gg)
        $('.desc').text(goodsInfo.content)
        $('.sales').text(goodsInfo.pay_yes)
        if(goodsInfo.canshu.length != 0){
            var span = ''
            for(var j in goodsInfo.canshu){
                span += `
                        <div class="item2">
                            <span>${goodsInfo.canshu[j].name}</span>
                            <span>${goodsInfo.canshu[j].text}</span>
                        </div>
                    `
            }
        }
        $("#parameter").html(span)
    })

    //同意、拒绝、取消精选
    $('#goodsList').on('click','.select',function(){
        var goodsId = $(this).data('id');
        var statusType = $(this).data('type');
        postAjax(`${global.apiUrl}/Api/Communityhealth/save_health_product_one`,function(res){
            console.log(res)
            if(res == 1){
              alert('操作成功')
              getGoodsList(page.p,page.status);
            }
        },{id: goodsId,status: statusType})
    })
});
